package com.youbar.client.controller;

import com.youbar.client.basic.ApiResult;
import com.youbar.client.dto.LoginDto;
import com.youbar.client.dto.user.RegisterDto;
import com.youbar.client.dto.user.StateDto;
import com.youbar.client.dto.user.UpdateInfoDto;
import com.youbar.client.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户信息类接口
 */
@RestController
@Slf4j
@RequestMapping(value = "/user", produces="application/json; charset=utf-8")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     * mobile=13608989898&password=111111&passwordAgain=111111&code123456
     * @param mobile
     * @param password
     * @param passwordAgain
     * @param code
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ApiResult register(@RequestParam String mobile, @RequestParam String password,
                              @RequestParam String passwordAgain, @RequestParam String code) {
        RegisterDto registerDto = new RegisterDto();
        registerDto.setMobile(mobile);
        registerDto.setPassword(password);
        registerDto.setPasswordAgain(passwordAgain);
        registerDto.setCode(code);
        return new ApiResult(userService.register(registerDto));
    }

    @GetMapping("/forgotPwd")
    public ApiResult forgotPwd() {
        RegisterDto registerDto = new RegisterDto();
        registerDto.setMobile("17687926669");
        registerDto.setPassword("111111");
        registerDto.setPasswordAgain("111111");
        registerDto.setCode("123456");
        return new ApiResult(userService.forgotPwd(registerDto));
    }

    /**
     * account=17687926669&password=111111
     * @param account
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ApiResult login(@RequestParam String account, @RequestParam String password) {
        LoginDto loginDto = new LoginDto();
        loginDto.setAccount(account);
        loginDto.setPassword(password);
        return new ApiResult(userService.login(loginDto));
    }

    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public ApiResult getInfo(HttpServletRequest request) {
        return new ApiResult(userService.getInfo(1));
    }

    @GetMapping("/updateInfo")
    public ApiResult updateInfo() {
        UpdateInfoDto updateInfoDto = new UpdateInfoDto();
        updateInfoDto.setId(7);
        updateInfoDto.setNickName("我是谁");
        updateInfoDto.setName("秋");
        updateInfoDto.setSex(2);
        updateInfoDto.setState(1);
        updateInfoDto.setMobile("17687929999");
        updateInfoDto.setPassword("123456");
        return new ApiResult(userService.updateInfo(updateInfoDto));
    }

    @GetMapping("/logOff")
    public ApiResult logOff() {
        StateDto stateDto = new StateDto();
        stateDto.setId(6);
        stateDto.setState(2);
        return new ApiResult(userService.updateState(stateDto));
    }

    @GetMapping("/delUser")
    public ApiResult delUser() {
        return new ApiResult(userService.deleteUser(5));
    }
}






